﻿using ConfClient;
using Google.Protobuf;
using Zpb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using UtilLib;
using UtilPublic.Serialization;
using SvCore.Tcp;

namespace client
{
    public partial class MainWindow
    {
        public void MsgHandle(MyPackage pack)
        {
            if (pack.ECode != EProtocolResult.成功)
            {
                App.DebugError(pack.ECode.ToString()); return;
            }
            var pid = CoreHash.HashMsg<L2CUserLogin>();
            if (pack.ProtocolId == pid)
            {
                pack.GetMsg(out L2CUserLogin rsp);
                GameData.UserId = rsp.UserId;
                GameData.Token = rsp.Token;
                {
                    C2GPlayerLogin req = new();
                    req.UserId = GameData.UserId;
                    req.Token = GameData.Token;
                    App.Send(req);
                }
            }
            pid = CoreHash.HashMsg<G2CPlayerReplace>();
            if (pack.ProtocolId == pid)
            {
                App.DebugError("被顶号"); return;
            }
            pid = CoreHash.HashMsg<G2CPlayerLogin>();
            if (pack.ProtocolId == pid)
            {
                pack.GetMsg(out G2CPlayerLogin rsp);
                if (rsp.Puid == 0)
                {
                    C2GPlayerCreate req = new();
                    req.UserId = GameData.UserId;
                    req.Token = GameData.Token;
                    req.Name = "8i9o0p";
                    var talent = 20;
                    req.Talent = new();
                    req.Talent.Str = talent;
                    req.Talent.Int = talent;
                    req.Talent.Con = talent;
                    req.Talent.Spi = talent;
                    req.Talent.Cps = talent;
                    req.Talent.Cor = talent;
                    req.Talent.Per = talent;
                    req.Talent.Kar = talent;
                    App.Send(req);
                    return;
                }
                else
                {
                    玩家进入游戏();
                }
            }
            pid = CoreHash.HashMsg<G2CPlayerCreate>();
            if (pack.ProtocolId == pid)
            {
                玩家进入游戏();
            }
            pid = CoreHash.HashMsg<G2CEnterGame>();
            if (pack.ProtocolId == pid)
            {
                pack.GetMsg(out G2CEnterGame rsp);
                GameData.PlayerInfoBase = rsp.InfoBase;
                GameData.PlayerInfoMisc = rsp.InfoMisc;
                GameData.PlayerInfoTalent = rsp.InfoTalent;
                GameData.PlayerInfoStatus = rsp.InfoStatus;
                GameData.PlayerInfoItems = rsp.InfoItems;
                GameData.PlayerInfoEquips = rsp.InfoEquips;
                玩家登录成功();
            }
            pid = CoreHash.HashMsg<G2CNtfPlayerInfo>();
            if (pack.ProtocolId == pid)
            {
                pack.GetMsg(out G2CNtfPlayerInfo rsp);
                GameData.PlayerInfoBase = rsp.InfoBase;
                GameData.PlayerInfoMisc = rsp.InfoMisc;
                App.PagePlayer.UpdatePage();
            }
            /*
            var pid = (EPid)msg.ProtocolId;
            switch (pid)
            {
                case EPid.Gcconnected:
                    {
                        App.Debug($"Connected");
                    }
                    break;
                case EPid.LcuserLogin:
                    {
                        L2CUserLogin rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.UserId = rsp.UserId;
                        GameData.Token = rsp.Token;
                        {
                            C2GPlayerLogin req = new();
                            req.UserId = GameData.UserId;
                            req.Token = GameData.Token;
                            App.Send(req);
                        }
                    }
                    break;
                case EPid.GcplayerLogin:
                    {
                        if (msg.code == EProtocolResult.玩家不存在)
                        {
                            C2GPlayerCreate req = new();
                            req.UserId = GameData.UserId;
                            req.Token = GameData.Token;
                            req.Name = "8i9o0p";
                            App.Send(req);
                            return;
                        }
                        获取玩家信息();
                    }
                    break;
                case EPid.GcplayerReplace:
                    {
                        App.DebugError("你的账号在其他设备登录！"); 
                    }
                    break;
                case EPid.GcplayerCreate:
                    {
                        获取玩家信息();
                    }
                    break;
                case EPid.GcplayerInit:
                    {
                        PlayerInit rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.PlayerInfoBase = rsp.InfoBase;
                        GameData.PlayerInfoMisc = rsp.InfoMisc;
                        GameData.ItemInfoList = rsp.InfoItems;
                        GameData.EquipInfoList = rsp.InfoEquips;
                        玩家登录成功();
                    }
                    break;
                case EPid.GcPlayerPushInfo:
                    {
                        PlayerInfo rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.PlayerInfoBase = rsp.InfoBase;
                        GameData.PlayerInfoMisc = rsp.InfoMisc;
                        App.PagePlayer.UpdatePlayerBase();
                        App.PagePlayer.UpdatePlayerMisc();
                    }
                    break;
                case EPid.GcPlayerPushInfoBase:
                    {
                        PlayerInfoBase rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.PlayerInfoBase = rsp;
                        App.PagePlayer.UpdatePlayerBase();
                    }
                    break;
                case EPid.GcPlayerPushInfoMisc:
                    {
                        PlayerInfoMisc rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.PlayerInfoMisc = rsp;
                        App.PagePlayer.UpdatePlayerMisc();
                    }
                    break;
                case EPid.GcmailListGet:
                    {
                        MailInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.MailInfoList = rsp;
                        App.PagePlayer.UpdateMailInfoList();
                    }
                    break;
                case EPid.GcitemInfoGet:
                case EPid.GcitemInfoPush:
                    {
                        ItemInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.ItemInfoList = rsp;
                        App.PagePlayer.UpdateItemInfo();
                    }
                    break;
                case EPid.GcequipInfoGet:
                case EPid.GcequipInfoPush:
                    {
                        EquipInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.Body));
                        GameData.EquipInfoList = rsp;
                        App.PagePlayer.UpdateEquipInfo();
                    }
                    break;

                default:
                    break;
            }
            */
        }
        private void 玩家进入游戏()
        {
            C2GEnterGame req = new();
            App.Send(req);
        }
        public void 玩家登录成功()
        {
            App.WdMain.ColumnGridNav.Width = new GridLength(100);
            App.WdMain.GridNav.Visibility = Visibility.Visible;
            //GameData.Player = rsp.Player;
            App.WdMain.PageOn("PagePlayer");
            //App.WdMain.PageOn("PageDebug");
            App.PagePlayer.UpdatePage();
        }
    }
}
